package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import com.ruoyi.system.domain.SysBmType;
import com.ruoyi.system.service.ISysBmFacilityService;
import com.ruoyi.system.service.ISysBmGoodsService;
import com.ruoyi.system.service.ISysBmTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 物资编码Controller
 *
 * @author yy
 * @date 2021-11-17
 */
@Api(tags = " 物资编码")
@RestController
@RequestMapping("/sysBmCode")
public class SysBmCodeController extends BaseController {
    @Autowired
    private ISysBmTypeService iSysBmTypeService;

    @Autowired
    private ISysBmFacilityService iSysBmFacilityService;

    @Autowired
    private ISysBmGoodsService iSysBmGoodsService;

    /**
     * 生成物资编码
     */
    @ApiOperation(value="生成物资编码", notes="生成物资编码")
    @PreAuthorize(hasPermi = "sysBmCode:sysBmCode:gen")
    @Log(title = "生成物资编码", businessType = BusinessType.INSERT)
    @PostMapping("/genCode")
    public SysBmType genCode(@RequestParam("sysBmId") Long sysBmId,@RequestParam("codeType") String codeType) {
        SysBmType sysBmType=null;
        if (codeType.equals(SysBmCodeTypeValue.FACILITY)){
            sysBmType=iSysBmFacilityService.selectSysBmTypeById(sysBmId);
            sysBmType=sysBmTypeAddOne(sysBmType);
            iSysBmFacilityService.updateSysBmType(sysBmType);
        }else if (codeType.equals(SysBmCodeTypeValue.GOODS)){
            sysBmType=iSysBmGoodsService.selectSysBmTypeById(sysBmId);
            sysBmType=sysBmTypeAddOne(sysBmType);
            iSysBmGoodsService.updateSysBmType(sysBmType);
        }else{
            sysBmType=iSysBmTypeService.selectSysBmTypeById(sysBmId);
            sysBmType=sysBmTypeAddOne(sysBmType);
            iSysBmTypeService.updateSysBmType(sysBmType);
        }
        return sysBmType;
    }

    /**
     * 生成物资编码
     */
    @ApiOperation(value="生成物资编码", notes="生成物资编码")
    @PreAuthorize(hasPermi = "sysBmCode:sysBmCode:gen")
    @Log(title = "生成物资编码", businessType = BusinessType.INSERT)
    @PostMapping("/genGoodsCode")
    public SysBmType genGoodsCode(@RequestParam("sysBmId") Long sysBmId, @RequestParam("materialName") String materialName) {
        SysBmType sysBmType=null;
        sysBmType=iSysBmGoodsService.selectSysBmTypeByParentId(sysBmId,materialName);
        if(sysBmType == null){
            sysBmType = new SysBmType();
            sysBmType.setName(materialName);
            sysBmType.setParentId(sysBmId);
            String value = this.genValue(sysBmId);
            sysBmType.setValue(value);
            sysBmType.setCode("0");
            SysBmType parent = iSysBmGoodsService.selectSysBmTypeById(sysBmId);
            sysBmType.setGenCode(parent.getGenCode()+"-" +value);
            iSysBmGoodsService.insertSysBmType(sysBmType);
        }
        sysBmType=sysBmTypeAddOne1(sysBmType);
        iSysBmGoodsService.updateSysBmType(sysBmType);
        return sysBmType;

    }
    private String genValue(Long sysBmId){
        String result = null;
        SysBmType search = new SysBmType();
        search.setParentId(sysBmId);
        List<SysBmType> list = iSysBmGoodsService.selectSysBmTypeList(search);
        if(list != null && list.size()>0){
            Optional<String> res = list.stream().map(SysBmType::getValue).max((e1, e2) -> e1.compareTo(e2));
            result = StringUtils.formatZeroEx((Integer.parseInt(res.get())+1),2);
        }else {
            result = "01";
        }

        return result;
    }


    @PostMapping("/getCodeByGenCode")
    @PreAuthorize(hasPermi = "sysBmCode:sysBmCode:gen")
    @Log(title = "生成物资编码", businessType = BusinessType.INSERT)
    public SysBmType getCodeByGenCode(@RequestParam(value = "genCode", required = false) String genCode, @RequestParam(value = "codeType", required = false) String codeType) {
        SysBmType query = new SysBmType();
        query.setGenCode(genCode);
        List<SysBmType> list = new ArrayList<>();
        if (codeType.equals(SysBmCodeTypeValue.FACILITY)) {
            //物料
            list = iSysBmFacilityService.selectSysBmTypeList(query);

        } else if (codeType.equals(SysBmCodeTypeValue.DEVICE)) {
            //设备
            list = iSysBmTypeService.selectSysBmTypeList(query);
        } else {
            list = iSysBmGoodsService.selectSysBmTypeList(query);
        }
        if (list.size() > 0) {
            return genCode(list.get(0).getBmId(), codeType);
        } else {
            return null;
        }
    }

    /**
     * 编码+1
     * @param sysBmType
     * @return
     */
    private SysBmType sysBmTypeAddOne(SysBmType sysBmType){
        int code= Integer.parseInt(sysBmType.getCode());
        code+=1;
        sysBmType.setCode(code+"");
        sysBmType.setNowCode(sysBmType.getGenCode()+"-"+StringUtils.formatZero(code));
        return sysBmType;
    }

    private SysBmType sysBmTypeAddOne1(SysBmType sysBmType){
        int code= Integer.parseInt(sysBmType.getCode());
        code+=1;
        sysBmType.setCode(code+"");
        sysBmType.setNowCode(sysBmType.getGenCode()+"-"+StringUtils.formatZeroEx(code,4));
        return sysBmType;
    }

    /**
     * 生成物资编码,返回所有信息
     */
    @ApiOperation(value="生成物资编码", notes="生成物资编码")
    @PreAuthorize(hasPermi = "sysBmCode:sysBmCode:gen")
    @Log(title = "生成物资编码", businessType = BusinessType.INSERT)
    @PostMapping("/genCodeWithAll")
    public Map genCodeWithAll(@RequestParam("sysBmId") Long sysBmId) {
        Map map=new HashMap();
        SysBmType sysBmType=iSysBmTypeService.selectSysBmTypeById(sysBmId);
        int code= Integer.parseInt(sysBmType.getCode());
        code+=1;
        sysBmType.setCode(code+"");
        iSysBmTypeService.updateSysBmType(sysBmType);
        map.put("code",sysBmType.getGenCode()+StringUtils.formatZero(code));
        map.put("bmInfo",sysBmType);
        return map;
    }

}
